package com.qiyun.dinghuohui.utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSONUtil {
	public static String[] extractArrayFromJsonArray(JSONArray array)
			throws JSONException {
		int len = array.length();
		String[] arr = new String[len];
		for (int i = 0; i < len; i++) {
			arr[i] = array.getString(i);
		}
		return arr;
	}

	public static List<Map<String, String>> extractRecordsFromJson(
			JSONArray array, String[] dataTag) throws JSONException {
		int len = array.length();
		List<Map<String, String>> list = new ArrayList<Map<String, String>>(len);

		for (int i = 0; i < len; i++) {
			JSONObject obj = (JSONObject) array.get(i);
			HashMap<String, String> one = new HashMap<String, String>();
			for (int j = 0; j < dataTag.length; j++) {
				one.put(dataTag[j], obj.getString(dataTag[j]));
			}
			one.put("id", obj.getString("id"));
			one.put("flag", obj.getString("flag"));

			list.add(one);
		}
		return list;
	}

	public static JSONObject createJSONObject(Map map) {
		return new JSONObject(map);
	}

	public static JSONArray createJSONArray(List list) {
		return new JSONArray(list);
	}
}
